package com.fpx.order.plat.bussiness.plat.amazon.task;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fpx.order.plat.bussiness.dao.log.IPullOrderLogDao;
import com.fpx.order.plat.bussiness.entity.CustomerShop;
import com.fpx.order.plat.bussiness.entity.PullOrderLog;
import com.fpx.order.plat.bussiness.entity.PullorderLogFail;
import com.fpx.order.plat.bussiness.plat.amazon.service.AmazonPlatOrderHandler;
import com.fpx.order.plat.bussiness.plat.base.PlatOrderCurrentJob;
import com.fpx.order.plat.bussiness.system.PlatConstants;
import com.fpx.order.plat.bussiness.vo.OrderDealResult;


/**
 * desc:
 * @author xiangtao(xiangtao@4px.com)
 * @date 2015年11月24日 下午4:38:04
 * @version 1.0
 */
@Component
public class AmazonReceiveOrderJob extends PlatOrderCurrentJob {
	
	private final static Logger LOGGER = Logger.getLogger(AmazonReceiveOrderJob.class);
	
	@Autowired
	private AmazonPlatOrderHandler amazonPlatOrderHandler;
	
	@Autowired
	private IPullOrderLogDao pullOrderLogDao;
	/**
	 * 定时任务
	 */
	public void exec(){
		LOGGER.info("【亚马逊amazon】 自动拉单开始.....");
		initMeodth(PlatConstants.ORDER_SOURCE_AM);
		List<CustomerShop> customerLst=super.getCustomers();
		PullOrderLog pullOrderLog=new PullOrderLog();
		if(!org.springframework.util.StringUtils.isEmpty(customerLst)){
			// 遍历商家和店铺
			for (CustomerShop customer : customerLst) {
				List<OrderDealResult> orderDealResultLst=null;
				String cm_id=customer.getCm_id();
				String cs_id=customer.getCs_id();//商铺ID
		        try {
		        	pullOrderLog.setFk_cm_id(customer.getCm_id());//客户id
					pullOrderLog.setFk_cs_code(customer.getCs_id());//商铺ID
					pullOrderLog.setCs_platform_code(PlatConstants.ORDER_SOURCE_AM);//平台号
					Date beginTime = new Date(); 
					pullOrderLog.setPull_date_begin(dateFormat.format(beginTime));//拉单开始时间
//					if(cm_id.equals("531221")&&cs_id.equals("479")){
						orderDealResultLst = amazonPlatOrderHandler.getOrderByCustomer(customer, null, null, 0);
				        if(orderDealResultLst==null||orderDealResultLst.size()<=0){
			    			throw new RuntimeException("【AMAZON】---商家：[" + cm_id + "]-商铺ID:["+cs_id+"],没有获取到订单");
			    		}
//					}
		       } catch (Exception ex) {
			        LOGGER.error("【AMAZON】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【异常信息】: "+ex.getMessage());
			        pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_N);//拉单失败
			        Date endTime = new Date(); 
					pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
					try{
						PullorderLogFail LogFail=new PullorderLogFail();
						LogFail.setError_msg(ex.getMessage());
						pullOrderLogDao.savePullOrderLog(pullOrderLog);
						LogFail.setFk_pol_id(pullOrderLog.getPol_id());
						pullOrderLogDao.savePullLogFail(LogFail);
					}catch(Exception e){
						 LOGGER.error("【AMAZON】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存AMAZON拉单失败日志时异常】  "+e.getMessage());
					}	
		       }
		        if(orderDealResultLst !=null&& orderDealResultLst.size()>0){
		          Date endTime = new Date(); 
				  pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
				  pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_Y);
				   try{
					 pullOrderLogDao.savePullOrderLog(pullOrderLog);
				   }catch(Exception e){
				     LOGGER.error("【AMAZON】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存AMAZON拉单成功日志时异常】  "+e.getMessage());
				   }  
		        }   
		    } 
		 }
		 LOGGER.info("【亚马逊amazon】 自动拉单结束");
	}
	
}
